<?php 


class Controller_City extends Controller_Website {

   
    public function action_list() {
        $m_city = Model::factory('city');
        
        $where = array();
        $where['ORDER'] = 'add_time DESC';
        $total = $m_city->count($where);
        $pager = new Pager($total, 20);
        $list = $m_city->select($pager->offset, $pager->size, $where)->as_array();
//         $list = $m_category->getAll($where)->as_array();
//         $cat_list = array_column($list, 'name', 'id');
//         foreach ($list as &$item) {
//             $item['parent_name'] = isset($cat_list[$item['parent_id']]) ? $cat_list[$item['parent_id']] : '';
//         }
        
        $this->content = View::factory('city/list');
        $this->content->list = $list;
        $this->content->pager = $pager;
    }
    
    public function action_add() {
        $m_city = Model::factory('city');
        $m_allCity = Model::factory('allCity');
        if (!empty($_POST)) {
            $data = $this->_get_data($_POST);
//             var_dump($data);exit;
            $data['add_time'] = time();
            $ret = $m_city->insert($data);
            if ($ret !== false) {
                $this->redirect('city/list');
            }
        }
        $city = $m_allCity::index();
// var_dump($city);exit;
        $this->content = View::factory('city/add');
        $this->content->city = $city;

    }

    public function action_del() {
        $id = $_GET['id'];
        $m_city = Model::factory('city');
        $ret = $m_city->deleteById($id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    

    protected function _get_data($post) {
        return array_intersect_key($post, array_flip(array('city','city_code','province')));
    }
    
}

